package com.evil.common.application.enums;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * 应用版本
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ApplicationVersionEnum {

    /**
     * 应用版本
     */
    RELEASE_VERSION(0, "发布版"),
    DESIGN_VERSION(1, "设计版"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<ApplicationVersionEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ApplicationVersionEnum findById(Integer id) {
        return ApplicationVersionEnum.isExistById(id).orElseThrow(() ->
                new BusinessException(RCodeEnum.ERROR_PARAMETER, "应用版本参数错误"));
    }

    public static List<ApplicationVersionEnum> getEnums() {
        return Arrays.asList(ApplicationVersionEnum.values());
    }

    public static void forEach(Consumer<ApplicationVersionEnum> consumer) {
        ApplicationVersionEnum.getEnums().forEach(consumer);
    }
}
